import React from 'react';

export function result(showErr = true) {
  const _this = {};
  _this.showErr = showErr;
  let callbacks = {};

  function parse(res) {
    if (res) {
      const {message, status} = res || {};
      if (_this.showErr && message && status === 400) {
        // Toast.fail(message, 1);
      }
      const {success, error} = callbacks;
      if (status === 200) {
        (typeof success === 'function') && success(res);
      } else {
        (typeof error === 'function') && error(res);
      }
    }
  }

  _this.success = function (callback) {
    callbacks['success'] = callback;
    return _this;
  };
  _this.error = function (callback) {
    callbacks['error'] = callback;
    return _this;
  };
  _this.end = function () {
    return function (res) {
      parse(res);
    }
  };
  return _this;
}
